package dtd.PHS.VND_ExchangeRate.widget;

import java.util.ArrayList;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import dtd.PHS.VND_ExchangeRate.MainActivity;
import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.data.DataProvider;
import dtd.PHS.VND_ExchangeRate.data.IDelegateDataGetter;
import dtd.PHS.VND_ExchangeRate.data.ListRates;
import dtd.PHS.VND_ExchangeRate.data.RateItem;
import dtd.PHS.VND_ExchangeRate.data.SJCItem;
import dtd.PHS.VND_ExchangeRate.data.VNExDatabase;
import dtd.PHS.VND_ExchangeRate.utils.Helpers;
import dtd.PHS.VND_ExchangeRate.utils.Logger;

public class UpdateWidgetService 
extends Service 
implements IDelegateDataGetter 
{

	private ListRates listrates;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	@Override
	public void onCreate() {
		super.onCreate();
		Helpers.initGlobalData(getApplicationContext());
	}

	/**
	 * Input: app widget ids inside the database
	 * Output: 
	 * 	- update widget immediately
	 * 	- Broadcast update widget intent 
	 * 
	 */
	@Override
	public void onStart(Intent intent, int startId) {	
		super.onStart(intent, startId);
		Logger.logInfo("Updatewidget service: started !");
		DataProvider.getInstance().requestListRates(this);
	}

	@Override
	public void onGetDataCompleted(Object data) {
		if ( data instanceof ListRates) {
			listrates = (ListRates) data;
			updateWidgets();
			stopSelf();
		}
	}

	private void updateWidgets() {
		//TODO: remember to remove widgets from DB in OnDelete...
		ArrayList<WidgetItem> listInfo = VNExDatabase.getAllWidgetInfo();
		for(WidgetItem item : listInfo ) {
			updateWidget( item );
		}
	}

	private void updateWidget(WidgetItem item) {
		int id = item.getId();
		String currency = item.getCurrency();		
		if ( currency.equals(SJCItem.SJC)) {

		} else {
			RateItem rate = null;
			for(RateItem rateItem : listrates) {
				if ( rateItem.getCurrencyName().equals(currency)) {
					rate = rateItem;
					break;
				}
			}
			updateCurrencyWidget(getApplicationContext(),id,rate);
		}
	}

	static public  void updateCurrencyWidget(Context context, int id, RateItem rate) {
		RateRemoteView rateRemoteView = new RateRemoteView(context);
		try {
			rateRemoteView.setRate(rate);
			Intent intent = new Intent(context,MainActivity.class);
			PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
			rateRemoteView.setOnClickPendingIntent(R.id.widget_layout, pIntent);
			AppWidgetManager.getInstance(context).updateAppWidget(id, rateRemoteView);
		} catch (Exception e) {
			Logger.logError(e);
		}
	}

	@Override
	public void onGetDataError(Exception e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDestroy() {
		Logger.logInfo("UpdateWidget service: died !");
		super.onDestroy();
	}
}
